package com.fakesmoker.aidawnda;

public class ThreadLocalExample {
    // 创建一个ThreadLocal变量
    //private static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 0);
    public static ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        threadLocal=ThreadLocal.withInitial(()->0);

        // 启动两个线程
        Thread thread1 = new Thread(() -> {
            threadLocal.set(1); // 设置线程1的线程本地变量
            System.out.println("Thread 1: " + threadLocal.get()); // 获取线程1的线程本地变量
        });

        Thread thread2 = new Thread(() -> {
            threadLocal.set(2); // 设置线程2的线程本地变量
            System.out.println("Thread 2: " + threadLocal.get()); // 获取线程2的线程本地变量
        });

        thread1.start();
        thread2.start();
    }
}
